
Imports std
Imports System.IO

Module occunum

    Public Const maxInputSector As Int32 = 1024
    Public Const charCount As Int32 = 65536

    Public Function charToIndex(ByVal c As Char) As UInt32
        Return Convert.ToInt32(c)
    End Function

    Public Sub findfrontAction(ByVal n As chartrie(Of Int32).node)
        n.value += 1
    End Sub

    Sub Main(ByVal args() As String)
        On Error Resume Next
        Dim istream As FileStream = Nothing
        Dim reader As StreamReader = Nothing
        Dim istreamSource As FileStream = Nothing
        Dim readerSource As StreamReader = Nothing
        Dim ostream As FileStream = Nothing
        Dim writer As StreamWriter = Nothing
        Dim input As chartrie(Of Int32) = Nothing
        Dim inputs As vector(Of String) = Nothing
        Dim line As String = Nothing
        Dim work As chartrie(Of Int32).iterator = Nothing
        Dim i As Int32

        If args.Length <> 3 Then
            raiseError("argument count error, source input output", errorHandle.errorType.critical)
        Else
            input = New chartrie(Of Int32)()
            inputs = New vector(Of String)
            istream = New FileStream(args(1), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
            reader = New StreamReader(istream)
            assert(Not (istream Is Nothing Or reader Is Nothing), "file no found, " + args(1))

            ostream = New FileStream(args(2), FileMode.Create, FileAccess.Write, FileShare.Read)
            writer = New StreamWriter(ostream)
            assert(Not (ostream Is Nothing Or writer Is Nothing), "file no found, " + args(2))

            While Not reader.EndOfStream
                input.clear()
                inputs.clear()
                For i = 0 To maxInputSector - 1
                    If Not reader.EndOfStream Then
                        line = reader.ReadLine
                        kickNewlineChar(line)
                        If Not isemptystring(line) Then
                            input.insert(line.ToCharArray, 0)
                            inputs.push_back(line)
                        End If
                    Else
                        Exit For
                    End If
                Next

                istreamSource = New FileStream(args(0), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                readerSource = New StreamReader(istreamSource)
                assert(Not (readerSource Is Nothing Or istreamSource Is Nothing), "file no found, " + args(0))
                While Not readerSource.EndOfStream
                    line = readerSource.ReadLine
                    For i = 0 To strlen(line) - 1
                        work = input.findfront(strmid(line, i).ToCharArray, AddressOf findfrontAction)
                    Next
                End While
                closestream(readerSource)

                For i = 0 To inputs.size() - 1
                    work = input.find(inputs(i).ToCharArray)
                    If Not work Is Nothing Then
                        writer.WriteLine(inputs(i) + character.tab + Convert.ToString((+work).value))
                    End If
                Next
            End While

            closestream(reader)
            closestream(writer)
        End If
    End Sub

End Module
